package com.dementors.ui.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.dementors.R;
import com.dementors.model.Goods;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * ***********************
 * author:raochen
 * time:2015/12/8.
 * name:
 * step:
 * ***********************
 */
public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ItemViewHolder> {
    private Context context;
    private List<Goods.Show> mList;
    private MyItemClickListener listener;

    public interface MyItemClickListener {
        void onItemClick(View v, int position);
    }
    public void  setMyItemClickListener(MyItemClickListener listener){
        this.listener=listener;
    }

    public ItemAdapter(Context context) {
        this.context = context;
        mList = new ArrayList<>();
    }

    public void setList(List<Goods.Show> List) {
        this.mList.clear();
        this.mList.addAll(List);
    }

    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemViewHolder holder = new ItemViewHolder(LayoutInflater.from(context).
                inflate(R.layout.item_recyclerview_itemactivity, parent, false), listener);
        return holder;
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, int position) {
        Goods.Show  good=mList.get(position);

        holder.tv_item_content.setText(good.title);
        holder.tv_item_price.setText("¥"+good.price/100);
        //字中间加横线
        holder.tv_item_oriprice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        holder.tv_item_oriprice.setText(good.price_ori/100+"");
        double a=(good.price*1.0/good.price_ori);
        int b=(int)(a*100);
        holder.tv_item_cut.setText(b*1.0/10+"折");
        Picasso.with(context).load(good.img).into(holder.iv_item_recyclerview);

    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        private TextView tv_item_content, tv_item_price, tv_item_oriprice, tv_item_cut;
        private ImageView iv_item_recyclerview;
        private MyItemClickListener mListener;

        public ItemViewHolder(View itemView, final MyItemClickListener listener) {
            super(itemView);
            this.mListener = listener;
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null) {
                        mListener.onItemClick(v, getPosition());
                    }
                }
            });
            tv_item_content= (TextView) itemView.findViewById(R.id.tv_item_content);
            tv_item_price= (TextView) itemView.findViewById(R.id.tv_item_price);
            tv_item_oriprice= (TextView) itemView.findViewById(R.id.tv_item_oriprice);
            tv_item_cut= (TextView) itemView.findViewById(R.id.tv_item_cut);
            iv_item_recyclerview= (ImageView) itemView.findViewById(R.id.iv_item_recyclerview);
        }


    }
}
